package com.yanqu.road.server.cmd.activity.tonbingchannel.channel;

import com.yanqu.road.entity.player.UserHistoryAttribute;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.bussiness.player.UserHistoryAttributeBussiness;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChannelTonBingProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;

import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_CHANNEL_CTON_BING_GET_UNION_MEMBER_REQ, desc = "")
@Deprecated
public class TonBingGetUnionMemberReqCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        ChannelTonBingProto.ChannelTonBingGetMemberReqMsg reqMsg = ChannelTonBingProto.ChannelTonBingGetMemberReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        List<Long> userIdList = reqMsg.getUserIdList();

        Map<Long, UserHistoryAttribute> attributeMap = null;
        attributeMap = UserHistoryAttributeBussiness.getUserHistoryAttributeMap(userIdList);

        ChannelTonBingProto.ChannelTonBingGetMemberRespMsg.Builder builder = ChannelTonBingProto.ChannelTonBingGetMemberRespMsg.newBuilder();
        builder.setActivityId(activityId);
        for (Long userId : userIdList) {
            UserInfo userInfo = UserMgr.getUserInfo(userId);
            if (userInfo != null) {

                // 仅用作查询，以跨服上的unionUid为准
                String unionUid = userInfo.getUnionUid();
                // 加入商会后还没上线，userInfo的似乎没刷新过来
                if (StringUtils.isNullOrEmpty(unionUid)) {
                    CrossUnionMember unionMember = CrossUnionMgr.getUnionMember(userInfo.getUserId());
                    if (unionMember != null) {
                        unionUid = unionMember.getUnionUid();
                    }
                }

                ChannelTonBingProto.CTonBingUnionMemberMsg.Builder tmp = ChannelTonBingProto.CTonBingUnionMemberMsg.newBuilder();
                tmp.setUserId(userId);
                tmp.setUnionUid(unionUid);
                tmp.setEarnSpeed(userInfo.getEarnSpeed().toString());
                CrossUnionMember member = CrossUnionMgr.getUnionMember(userId);
                if(member == null){
                    ChannelTonBingMgr.getLogger().error(unionUid + "商会没有找到" + userId + "玩家的信息");
                    continue;
                }
                tmp.setPosition(member.getPosition());
                if (attributeMap.containsKey(userId)) {
                    tmp.setPatronsTopAbility(attributeMap.get(userId).getAbility().toString());
                }
                builder.addMember(tmp);
            }
        }

        GamePlayerMgr.sendChannelPlayerPacketEx(ChannelProtocol.N_TON_BING_GET_UNION_MEMBER_RESP, builder,0);
    }
}
